//
//  SWStatus.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/3/3.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit

class SWStatus: NSObject {
    
    // 微博创建时间
    var created_at: String?
    
    // 微博ID
    var id:	Int = 0
    
    // 微博信息内容
    var text: String?
    
    // 微博来源
    var source: String? {
        didSet{
            source = source?.sinaSource()
        }
    }
    
    // 微博配图: 数组里面放的是字典
    var pic_urls: [[String: AnyObject]]? {
        didSet {
        
            let count = pic_urls?.count ?? 0
            if count == 0 {return}
            //创建数组保存图片的URL地址
            storePictureURLs = [NSURL]()
            for dict in pic_urls! {
            
                let urlStrg = dict["thumbnail_pic"] as! String
                let url = NSURL(string: urlStrg)
                storePictureURLs?.append(url!)
            }
        }
    }
    
    var storePictureURLs: [NSURL]?
    
    // 转发数
    var reposts_count: Int = 0
    
    // 评论数
    var comments_count: Int = 0
    
    // 表态数
    var attitudes_count: Int = 0
    
    //用户
    var user: SWUser?
    
    //被转发的微博
    var retweeted_status: SWStatus?
    
    
    init(dict: [String : AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //要判断返回的字段是不是user 是的话要字典转模型
        if key == "user" {
            
            if let dict = value as? [String : AnyObject] {
                //字典转模型
                user = SWUser(dict: dict)
            }
            return
        }else if  key == "retweeted_status" { //被转发微博
            if let dict = value as? [String : AnyObject] {
                //字典转模型
                retweeted_status = SWStatus(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    // 打印
    override var description: String {
        let keys = ["created_at", "id", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count","user"]
        
        // \n 换行
        // \t: 制表符 = table键
        return "\n\t微博模型: \(dictionaryWithValuesForKeys(keys).description)"
    }
    
}
